package com.orangelife.mobile.hourlyEmployee.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.IXListViewListener;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.hourlyEmployee.adapter.OrderListAdapter;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;

public class OrderListActivity extends OrangeLifeBaseActivity {

    /** 标题中间 */
    private TextView tvTitle;
    /** 标题右边 */
    private TextView tvTitleRight;
    /** 列表 */
    private PullToRefreshListView lvList;
    /** 分辨是刷新还是加载更多 */
    private int flags = 1;
    /** 一页中数据的条数，初始设置为7条 */
    private int pageSize = Constant.PAGE_SIZE_DEFAULLT;
    /** 初始页，初始化为第一页 */
    private int pageNo = Constant.PAGE_NO_DEFAULT;
    private Dialog dialog;
    private OrderListAdapter<Map<String, Object>> adapter;
    private Handler mHandler;
    private List<Map<String, Object>> list;
    private List<Map<String, Object>> list2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hourly_employee_order_list);

    }

    @Override
    protected void onResume() {
        super.onResume();
        initeView();
        findView();
    }

    /**
     * 初始化
     */
    private void initeView() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitleRight = (TextView) findViewById(R.id.tv_title_right);
        lvList = (PullToRefreshListView) findViewById(R.id.lvRepair);
    }

    /**
     * 赋值
     */
    private void findView() {
        tvTitle.setText(R.string.my_hourly_employee);
        tvTitleRight.setVisibility(View.INVISIBLE);
        mHandler = new Handler();

        getDataFromServer();
        pageNo = 1;
        getList(pageSize, pageNo);
        setAdapter();
        lvList.setPullLoadEnable(false);
        lvList.setAdapter(adapter);
        pullListView();
    }

    /**
     * 设置适配器
     */
    private void setAdapter() {
        list = new ArrayList<Map<String, Object>>();
        list2 = new ArrayList<Map<String, Object>>();
        adapter = new OrderListAdapter<Map<String, Object>>(this,
                list);
        lvList.setPullLoadEnable(false);
        lvList.setAdapter(adapter);
    }

    /**
     * 下拉刷新列表
     */
    private void pullListView() {
        lvList.setXListViewListener(new IXListViewListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // list.clear();
                        flags = 1;
                        pageNo = 1;
                        getList(pageSize, pageNo);
                        onLoad();
                        adapter.setList(list);
                        adapter.notifyDataSetChanged();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        flags = 0;
                        pageNo += 1;
                        getList(pageSize, pageNo);
                        onLoad();
                        if (list != null) {
                            adapter.setList(list);
                            adapter.notifyDataSetChanged();
                        }
                    }
                }, 2000);
            }
        });
    }

    /**
     * 刷新 拉下列表
     *
     * @param list2
     */
    private void updateListView(List<Map<String, Object>> list2) {
        if (list2 == null) {
            lvList.setPullLoadEnable(false);
            if (flags == 1) {
                list.clear();
                adapter.setList(list);
                adapter.notifyDataSetChanged();
            }
        }
        if (list2 != null && list != null) {
            if (list2.size() == 0) {
                if (flags == 1) {
                    list.clear();
                    lvList.setPullLoadEnable(false);
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                } else {
                    lvList.setPullLoadEnable(false);
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                }
            } else {
                if (flags == 1) {
                    list.clear();
                    lvList.setPullLoadEnable(true);
                    list.addAll(list2);
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                } else {
                    lvList.setPullLoadEnable(true);
                    list.addAll(list2);
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                }
            }
        } else {
            lvList.setPullLoadEnable(false);
            if (flags == 1) {
            }
        }
    }

    /**
     * 下拉刷新的进度条
     */
    private void getDataFromServer() {
        dialog = DialogHelper.getInstance().createLoadingDialog(this,
                Constant.LOADING);
        dialog.show();
    }

    /**
     * 获取列表数据
     *
     * @param pageSize 每页的条数
     * @param pageNo   初始页
     */
    private void getList(int pageSize, int pageNo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageIndex", pageNo);
        map.put("pageSize", pageSize);
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, Constant.URL_HOURLY_EMPLOYEE_LIST, map, handler, 0, HttpUtil.ACTION_URI);
    }

    /**
     * 处理获得的数据
     */
    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    list2 = JsonReponseHandler.getListFromJson(
                            String.valueOf(msg.obj), adapter, dialog);

                    updateListView(list2);
                    break;
                case 500:
                    DialogHelper.closeDialog(dialog);
                    ToastHelper.getInstance()._toast(
                            msg.obj.toString());
                    break;
                case 1011:
                    isLogin(dialog);
                    break;

                default:
                    break;
            }
        }

    };


    @SuppressWarnings("deprecation")
    private void onLoad() {
        lvList.stopRefresh();
        lvList.stopLoadMore();
        lvList.setRefreshTime(new Date().toLocaleString());
    }


}
